import {
    CompleteState,
    AnswerState,
    Cardinality,
    BaseType,
    QtiCompletionStatus
} from './ServiceConst'


import ServiceUtils from './ServiceUtils'


/**
 * 适配正确答案模型
 * @param {*} correctAnswer 
 */
let parseCorrectAnswer = function (correctAnswer) {

    let correct = {
        baseType: BaseType.STRING, //默认都作为string类型处理
        cardinality: Cardinality.MULTIPLE,
        value: []
    }

    if (correctAnswer && correctAnswer.value) {
        correct.value = ServiceUtils.deepClone(correctAnswer.value);

        if (correctAnswer.baseType === 'multipleString') {
            correct.baseType = BaseType.MULTIPLE_STRING;
        }

        if (correctAnswer.cardinality === 'single') {
            correct.cardinality = Cardinality.SINGLE;
        } else if (correctAnswer.cardinality === 'ordered') {
            correct.cardinality = Cardinality.ORDERED;
        } else if (correctAnswer.cardinality === 'multiple') {
            correct.cardinality = Cardinality.MULTIPLE;
        }

    }
    return correct;
}

let convertTextEntry = function (qtiModel, firstModelId) {
    let modelMap = qtiModel.modelMap,
        hintFeedback = qtiModel.hintFeedback || {},
        answerFeedback = qtiModel.answerFeedback || {},
        item = {
            id: firstModelId,
            qti: true,
            type: 'textEntryMultipleInteraction', //题目类型type
            name: '', //题目名称
            subjective: false, //是否主观题
            hint: {
                content: ''
            },
            analysis: {
                content: ''
            },
            correctAnswer: {
                cardinality: Cardinality.ORDERED, //答案类型
                baseType: BaseType.STRING, //答案项类型
                value: [] //数组的形式存在
            },
            detail: qtiModel //具体题目详细内容
        }

    for (var id in modelMap) {
        if (modelMap.hasOwnProperty(id)) {
            item.correctAnswer.value.push(qtiModel.correctAnswer[id].value[0])
        }
    }

    if (hintFeedback[firstModelId] && hintFeedback[firstModelId].content !== undefined) {
        item.hint.content = hintFeedback[firstModelId].content;
    }
    if (answerFeedback[firstModelId] && answerFeedback[firstModelId].content !== undefined) {
        item.analysis.content = answerFeedback[firstModelId].content;
    }


    return item;

}


var QtiAdapter = {
    /**
     * 获取作答结果模型
     * 
     * @param {any} model 标准模型
     * @param {any} userAnswer 用户答案  
     * @returns 返回值 {
                answerState: true, //正确错误
                completeState: CompleteState.UNANSWERED, //是否作答
                unAnsweredCount: 0, //
                items: {}
            }
     */
    parseModel(qtiModel) {
        let items = [];

        let modelMap = qtiModel.modelMap,
            hintFeedback = qtiModel.hintFeedback || {},
            answerFeedback = qtiModel.answerFeedback || {},
            correctAnswer = qtiModel.correctAnswer,
            id,
            qtiItemModel,
            modelType,
            item,
            isTextEntry = true, //是否是填空题
            firstModelId = ''


        for (id in modelMap) {
            if (modelMap.hasOwnProperty(id)) {
                qtiItemModel = modelMap[id];
                modelType = qtiItemModel.modelType;
                if (!firstModelId) {
                    firstModelId = id;
                }
                if (modelType !== 'textEntryInteraction') {
                    isTextEntry = false;
                }
                item = {
                    id: id, //习题ID
                    qti: true,
                    type: modelType, //题目类型type
                    name: '', //题目名称
                    subjective: false, //是否主观题
                    hint: {
                        content: ''
                    },
                    analysis: {
                        content: ''
                    },
                    correctAnswer: {
                        cardinality: "", //答案类型
                        baseType: "", //答案项类型
                        value: [] //数组的形式存在
                    },
                    detail: {} //具体题目详细内容
                }


                if (hintFeedback[id] && hintFeedback[id].content !== undefined) {
                    item.hint.content = hintFeedback[id].content;
                }
                if (answerFeedback[id] && answerFeedback[id].content !== undefined) {
                    item.analysis.content = answerFeedback[id].content;
                }

                if (modelType === 'choiceInteraction_vote' ||
                    modelType === 'extendedTextInteraction' ||
                    modelType === 'drawingInteraction_drawing' ||
                    modelType === 'drawingInteraction' ||
                    modelType === 'drawingInteraction_handwrite' ||
                    (modelType === 'choiceInteraction' && qtiItemModel.questionType === 'vote')) {
                    item.subjective = true;
                }

                if (correctAnswer[id]) {
                    item.correctAnswer = parseCorrectAnswer(correctAnswer[id])
                }
                item.detail = qtiItemModel;

                items.push(item);
            }
        }

        return items;
    },
    parseAnswerState(answerState) {
        let result = {
                answerState: answerState.completionStatus === QtiCompletionStatus.PASSED, //正确错误
                items: {}
            },
            item,
            allUnAnswered = true,
            itemCount = answerState.responseVariable.length; //小题数量



        //Qti存在多题情况：填空题、选做题、复合题
        // TODO:答案为空时，是否是正确，需要确认
        answerState.responseVariable.forEach(model => {
            item = {
                id: model.identifier,
                unAnsweredCount: 0,
                answerState: model.state === QtiCompletionStatus.PASSED,
                completeState: CompleteState.ALL_ANSWERED,
                candidateResponse: ServiceUtils.deepClone(model.candidateResponse), //用户答案
                correctResponse: ServiceUtils.deepClone(model.correctResponse) //正确答案
            };

            let answeredCount = item.candidateResponse.filter(answer => {
                return answer !== '';
            }).length;



            item.unAnsweredCount = item.correctResponse.length - answeredCount;
            if (item.unAnsweredCount < 0) {
                item.unAnsweredCount = 0;
            }

            if (answeredCount === 0) {
                item.completeState = CompleteState.UNANSWERED;
            } else if (answeredCount === item.correctResponse.length || (item.correctResponse.length === 0 && answeredCount > 0)) {
                item.completeState = CompleteState.ALL_ANSWERED;
            } else {
                item.completeState = CompleteState.PARTICAL_ANSWERED;
            }

            if (item.completeState === CompleteState.UNANSWERED) {
                item.answerState = false;
            } else {
                allUnAnswered = false;
            }

            result.items[model.identifier] = item;

        });

        if (allUnAnswered) {
            result.answerState = false;
        }

        return result;

    },
    isQtiAnswerState(answerState) {
        return !!(answerState && answerState.completionStatus && answerState.responseVariable);
    }
}

export default QtiAdapter